package demo.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import demo.Member;
import demo.MemberService;
import demo.UserName;

public class DemoApplicationServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException
    {
        doService(request, response);
    }

    private void doService(HttpServletRequest request, HttpServletResponse response) throws IOException {
        final UserName userName = DemoSpringApplicationContextUtil.getBean("userName", UserName.class);
        if (request.getPathInfo().contains("register")) {
            MemberService service = DemoSpringApplicationContextUtil.getBean("memberService", MemberService.class);
            Member member = service.findMember(Integer.parseInt(request.getParameter("memberId")));
            userName.setName(member.getName());
        }
        response.getOutputStream().print("You are " + userName.getName());
    }

}